<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    public function authenticate()
    {
        $user = User::model()->findAll(array(
            'condition' => 'username = :username',
            'params' => array(':username' => $this->username)
        ));
        if (!isset($user)) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else {
            $user = User::model()->findAll(array(
                'condition' => 'username = :username AND password = :password',
                'params' => array(
                    ':username' => $this->username,
                    ':password' => $this->password)
            ));
            if (!isset($user)) {
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            } else {
                $this->errorCode = self::ERROR_NONE;
            }
        }
        return !$this->errorCode;
    }
}